import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';

/// 厦门大学计算机专业 | 前华为工程师
/// 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
/// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
/// 公众号：蓝不蓝编程

void main() => runApp(HspApp());

class HspApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HspPage(),
    );
  }
}

class HspPage extends StatefulWidget {
  const HspPage({Key? key}) : super(key: key);

  @override
  _HspPageState createState() => _HspPageState();
}

class _HspPageState extends State<HspPage> {
  final interval = const Duration(milliseconds: 100);
  var ball1 = "00";
  var ball2 = "02";
  var ball3 = "00";
  var ball4 = "00";
  var ball5 = "00";
  var ball6 = "00";
  var ball7 = "00";
  var swatch = Stopwatch();
  var isStarted = false;
  var timer;

  void startTimer() {
    getRedBall();
    swatch.start();
    timer = Timer.periodic(interval, (timer) {
      changeBalls();
    });
    setState(() {
      isStarted = true;
    });
  }

  void pauseTimer() {
    timer.cancel();
    swatch.stop();
    setState(() {
      isStarted = false;
    });
  }

  void changeBalls() {
    setState(() {
      ball1 = getRedBall();
      ball2 = getRedBall();
      ball3 = getRedBall();
      ball4 = getRedBall();
      ball5 = getRedBall();
      ball6 = getRedBall();
      ball7 = getBlueBall();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("双色球-蓝不蓝编程"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              buildRedBall(ball1),
              buildRedBall(ball2),
              buildRedBall(ball3),
              buildRedBall(ball4),
              buildRedBall(ball5),
              buildRedBall(ball6),
              buildBlueBall(ball7),
            ],
          ),
          const Padding(padding: EdgeInsets.all(40)),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[buildPlayPauseBtn()],
          )
        ],
      ),
    );
  }

  Container buildRedBall(String ball1) {
    return Container(
        width: 40,
        height: 40,
        padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
        child: Text(ball1,
            style: const TextStyle(
              color: Colors.white,
              fontSize: 20,
            )),
        decoration: const BoxDecoration(
          color: Colors.red,
          shape: BoxShape.circle,
        ));
  }

  Container buildBlueBall(String ball1) {
    return Container(
        width: 40,
        height: 40,
        padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
        child: Text(ball1,
            style: const TextStyle(
              color: Colors.white,
              fontSize: 20,
            )),
        decoration: const BoxDecoration(
          color: Colors.blue,
          shape: BoxShape.circle,
        ));
  }

  FloatingActionButton buildPlayPauseBtn() {
    if (isStarted) {
      return FloatingActionButton(
        child: const Icon(Icons.pause, color: Colors.white, size: 40),
        backgroundColor: Colors.yellow,
        onPressed: pauseTimer,
      );
    } else {
      return FloatingActionButton(
        child: const Icon(Icons.play_arrow, color: Colors.white, size: 40),
        backgroundColor: Colors.green,
        onPressed: startTimer,
      );
    }
  }

  getRedBall() {
    return getRandomNum(33);
  }

  getBlueBall() {
    return getRandomNum(16);
  }

  getRandomNum(maxNum) {
    var random = Random();
    var num = random.nextInt(maxNum);
    var result = num.toString();
    if (num < 10) {
      result = "0" + num.toString();
    }
    return result;
  }
}
